/////////////////////////////////////////////////////
//
//	File: lib.gant
//	Author: Gary Niu
//	Last Modified: 20-Mar-2008
//
/////////////////////////////////////////////////////


import org.yekki.utils.YekkiUtils
import java.text.SimpleDateFormat
import java.lang.reflect.*
import java.sql.*

f_exception = { msg = ''->
	
	ant.echo( level:'error', message:msg )
	System.exit( 1 )
}

f_exists = { path->

	path ? ( new File( path ) ).exists() : false
}

f_overwrite = { msg, path->

	if ( f_exists( path ) ) {
		
		ant.input( message:msg, validargs:'y,n', addproperty:'input.overwrite' )

		if ( 'n' == ant.antProject.properties.'input.overwrite') System.exit( -1 )
	}
}

f_bind = { moduleName = g_moduleName->
	
	def instName = ( g_config.yekki.modules."${moduleName}".global.settings + g_config."${moduleName}".global.settings ).current
	
	if ( g_config."${moduleName}".instances.containsKey( g_yekkiArg1 ) ) instName = g_yekkiArg1
	
	binding.setVariable( "g_${moduleName}Path", "${g_yekkiWorkbenchHome}/${moduleName}" )
	binding.setVariable( "g_${moduleName}ScriptPath", "${g_yekkiHome}/scripts/tasks/${moduleName}" )
	binding.setVariable( "g_${moduleName}BackupPath", "${g_yekkiWorkbenchHome}/backup/${moduleName}" )
	
	def bags = f_inst( moduleName, instName )
	
	bags.name = instName

	if ( bags.props.containsKey( 'ListenAddress' ) && bags.props.containsKey( 'ListenPort' ) ) bags.props.Url = "${bags.props.ListenAddress}:${bags.props.ListenPort}"
	
	binding.setVariable( "g_${moduleName}InstPath", "${g_yekkiWorkbenchHome}/${moduleName}/${instName}" )
	binding.setVariable( "g_${moduleName}InstBackupPath", "${g_yekkiWorkbenchHome}/backup/${moduleName}/${instName}" )
	binding.setVariable( "g_${moduleName}Inst", bags )
}

f_inst = { moduleName, instName->

	def props = g_config.yekki.modules."${moduleName}".instance.props + g_config."${moduleName}".instances."${instName}".props
	def settings = g_config.yekki.modules."${moduleName}".instance.settings + g_config."${moduleName}".instances."${instName}".settings
	def defs = g_config.yekki.modules."${moduleName}".instance.definations + g_config."${moduleName}".instances."${instName}".definations

	[ 'props':props, 'settings':settings, 'defs':defs ]
}

f_wlst = { scriptContent->

	def file = File.createTempFile( 'yekki', '.py' )
	
	file << scriptContent
	
	ant.wlst( filename:file.path )
}

f_shell = { path, script, arguments=''->

	def shell = f_iswin() ? g_config.yekki.global.shell.windows : g_config.yekki.global.shell.unix
	
	ant.pathconvert( property:'scriptPath', targetos:shell.targetos ) {
			
		ant.path( path:path )
	}

	if ( !script ) shell.suffix = ''
	if ( !path ) path = '.'

	ant.exec( dir:ant.antProject.properties.'scriptPath', executable:shell.executable, spawn:'true', newenvironment:'true' ) {
			
		arg( line:"${shell.prefix} ${script}${shell.suffix} ${arguments}" )
	}
}

f_iswin = {

	System.getProperty( "os.name" ).toUpperCase().indexOf( "WINDOWS" ) != -1 ? true : false
}

f_dir = { root, dirs->

	ant.mkdir( dir:root )
	
	dirs.keySet().each {
		
		def val = dirs."${it}"
		
		if ( val instanceof Map ) {
			
			def path = "${root}/${it}"
			
			ant.mkdir( dir:path )
			if ( dirs."${it}".size() > 0 ) f_dir( path, dirs."${it}" )
		}
		else {
			def filepath = "${root}/${it.replace( '__', '.' )}"
			ant.touch( file:filepath )
			( new File( filepath ) ) << val
		}
	}
}

f_wait = { moduleName = g_moduleName, waitstartup->

	def inst = binding.getVariable( "g_${moduleName}Inst" )
	def host = inst.props.ListenAddress
	def port = inst.props.ListenPort
	def success = g_msg.getMsg( 'lib.f_wait.startup_success', moduleName )
	def fail = g_msg.getMsg( 'lib.f_wait.startup_timeout', moduleName )
	
	ant.waitfor( maxwait:'3', maxwaitunit:'minute', checkevery:'5', checkeveryunit:'second', timeoutproperty:'timeout' ) {
		
		if ( waitstartup ) {

			ant.socket( server:host, port:port )
		}
		else {
			
			not() {
				ant.socket( server:host, port:port )
			}

			success = g_msg.getMsg( 'lib.f_wait.shutdown_success', moduleName )
			fail = g_msg.getMsg( 'lib.f_wait.shutdown_timeout', moduleName )
		}
	}

	if ( ant.antProject.properties.'timeout' ) {
		
		ant.echo fail
	}
	else {
		ant.echo success
	}
}